package com.aquan.map_jetpack.ui.screens

import android.content.Context
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalClipboardManager
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.text.input.VisualTransformation
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.aquan.map_jetpack.R
import com.aquan.map_jetpack.compositionLocal.LocalUserViewModel
import com.aquan.map_jetpack.ui.components.Loading
import com.google.accompanist.systemuicontroller.rememberSystemUiController
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import java.net.PasswordAuthentication

@Composable
fun LoginScreen(onNavigateTo: () -> Unit = {}, context: Context = LocalContext.current) {

    val systemUiController = rememberSystemUiController()

    // 用户信息
    val userViewModel = LocalUserViewModel.current



    LaunchedEffect(key1 = Unit) {
        systemUiController.setStatusBarColor(Color.Transparent)
    }

    var screenWidth: Float
    var screenHeight: Float
    with(LocalDensity.current) {
        screenWidth = LocalConfiguration.current.screenWidthDp.dp.toPx()
        screenHeight = LocalConfiguration.current.screenHeightDp.dp.toPx()

    }

    //用户名称
    var userName by remember {
        mutableStateOf("")
    }

    var passWord by remember {
        mutableStateOf("")
    }

    var showPassWord by remember {
        mutableStateOf(false)
    }


    val coroutineScope = rememberCoroutineScope()

    var status by remember {
        mutableStateOf(false)
    }

    val scaffoldState = rememberScaffoldState()

    Box(
        modifier = Modifier.fillMaxSize(),
    ) {
//        Image(
//            painter = painterResource(id = R.drawable.back),
//            contentDescription = null,
//            modifier = Modifier.fillMaxSize(),
//            contentScale = ContentScale.Crop
//        )

        Box(
            modifier = Modifier
                .fillMaxSize()
                .background(
                    Brush.linearGradient(
                        listOf(
                            Color(0xFF4BC2E6), Color.Transparent
                        ), start = Offset(
                            screenWidth,
                            0F
                        ), end = Offset(0F, screenHeight)
                    )
                )
        )

        Box(modifier = Modifier.fillMaxSize()) {
            if (status) {
                Loading()
            } else {
                null
            }
        }

        // 垂直布局
        Column(
            modifier = Modifier
                .fillMaxSize()
                .padding(8.dp),
            horizontalAlignment = Alignment.CenterHorizontally,
            verticalArrangement = Arrangement.SpaceBetween
        ) {
            Spacer(modifier = Modifier.height(100.dp))



            Text(
                text = "用户登录",
                color = Color.White,
                fontSize = 20.sp,
                fontWeight = FontWeight.Bold
            )

            Column(
                modifier = Modifier.fillMaxWidth(),
                horizontalAlignment = Alignment.CenterHorizontally
            ) {


                //用户名
                TextField(
                    value = userName, onValueChange = {
                        userName = it
                    },

                    singleLine = true,
                    leadingIcon = {
                        Icon(
                            imageVector = Icons.Default.Person, contentDescription = null,
                            tint = Color.White
                        )
                    },
                    label = {
                        Text(text = "用户名", fontSize = 14.sp, color = Color.White)
                    },
                    colors = TextFieldDefaults.textFieldColors(
                        backgroundColor = Color.Transparent,
                        focusedIndicatorColor = Color.LightGray,
                        unfocusedIndicatorColor = Color.LightGray,
                        focusedLabelColor = Color.LightGray,
                        unfocusedLabelColor = Color.LightGray,
                        cursorColor = Color.White
                    )
                )
                // 密码
                //用户名
                TextField(
                    value = passWord, onValueChange = {
                        passWord = it
                    },

                    singleLine = true,
                    leadingIcon = {
                        Icon(
                            imageVector = Icons.Default.Password, contentDescription = null,
                            tint = Color.White
                        )
                    },
                    trailingIcon = {
                        Icon(
                            imageVector = if (showPassWord) Icons.Default.Visibility else Icons.Default.VisibilityOff,
                            contentDescription = null,
                            modifier = Modifier.clickable {
                                showPassWord = !showPassWord
                            },
                            tint = Color.White
                        )
                    },

                    label = {
                        Text(text = "用户密码", fontSize = 14.sp, color = Color.White)
                    },
                    visualTransformation = if (showPassWord) VisualTransformation.None else PasswordVisualTransformation(),
                    colors = TextFieldDefaults.textFieldColors(
                        backgroundColor = Color.Transparent,
                        focusedIndicatorColor = Color.LightGray,
                        unfocusedIndicatorColor = Color.LightGray,
                        focusedLabelColor = Color.LightGray,
                        unfocusedLabelColor = Color.LightGray,
                        cursorColor = Color.White
                    )
                )

                Spacer(modifier = Modifier.height(8.dp))

                TextButton(onClick = {

//                    coroutineScope.launch {
//                        status = !status
////                        scaffoldState.snackbarHostState.showSnackbar("Snackbar")
//                        userViewModel.login(
//                            context = context,
//                            userName = userName,
//                            password = passWord,
//                            onClose = {
//                                onNavigateTo.invoke()
//                            })
//                    }
//                    onNavigateTo.invoke()
                }) {
                    Text(text = "登录", color = Color.White, fontSize = 14.sp)
                }
            }
            TextButton(onClick = { /*TODO*/ }) {
                Text(text = "还没有账号?立即注册", color = Color.LightGray, fontSize = 14.sp)
            }
            Spacer(modifier = Modifier.height(10.dp))


        }

    }


}


@Preview
@Composable
fun LoginScreenPreview() {
    LoginScreen()
}

